﻿import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;

public class MaxLengthTextDocument extends PlainDocument 
{

	    //Store maximm characters permitted

	    private int maxChars;

	    @Override

	    public void insertString(int offs, String str, AttributeSet a)

	        throws BadLocationException {

	        if(str != null && (getLength() + str.length() < maxChars)){
	        super.insertString(offs, str, a);

	        }
	    }
	    void setMaxChars(int a)
	    {
	    	maxChars = a;
	    }
	    //getter e setter omitted

	}